package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.untils.UiNameConst;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * scrollbar
	 * @author liangqian
	 */
	public class VScrollBar extends Component
	{
		/**
		 * 
		 */		
		public static const STYLE1:int = 1;
		public static const STYLE2:int = 2;
		public static const STYLE3:int = 3;
		/**
		 * 自动隐藏 参数
		 */
		private static const TIME_FADE_DURING:Number    = .3;
		private static const TIME_FADE_OUT_DELAY:Number = 3;

		private var _value:Number                 = 0;
		private var _max:Number                   = 1;
		private var _pageSize:Number              = 1;
		private var _min:Number                   = 0;

//		/**
//		 * 最大值 大于 一页的数目时
//		 * 滚动条才显示
//		 */
//		private var _needShow:Boolean;
		private var dragRect:Rectangle;

		private var thumb:MovieClip;
//		private var thumbIcon:Bitmap;

		private var onChange:Function;
		
		private var bg:MovieClip;
		private var _style:int;

		/**
		 * setSize
		 * setSliderParams
		 * catchContent
		 *
		 * set needShow
		 * handler()
		 */
		public function VScrollBar(parent:DisplayObjectContainer , xpos:int , ypos:int , handler:Function,style:int = 1)
		{
			_style = style;
			super(parent , xpos , ypos);
			onChange = handler;
		}

		override protected function init():void
		{
			setSize(15 , 202);
			super.init();
			dragRect = new Rectangle;
		}

		override protected function addChildren():void
		{
			super.addChildren();
			bg = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'bg')as Class)as MovieClip;
			bg.gotoAndStop(_style);
			bg.buttonMode = true;
			addChild(bg);
			thumb = new (DllLoaderManager.getInstance().getMaterial(UiNameConst.GENERAL_ICON,'arrow')as Class)as MovieClip;
			thumb.gotoAndStop(_style);
//			thumbIcon = new Bitmap;
//			thumb.addChild(thumbIcon);
			thumb.buttonMode = true;
			addChild(thumb);
			bg.x = (thumb.width - bg.width)*.5;
			thumb.addEventListener(MouseEvent.ROLL_OVER , overThumbHanlder);
			thumb.addEventListener(MouseEvent.MOUSE_DOWN , dragThumbHanlder);
			bg.addEventListener(MouseEvent.CLICK , clickHandler);
		}

		private function correctValue():void
		{
			_max = Math.max(_max , _min);
			_value = Math.max(Math.min(_value , _max) , _min);
		}

		///////////////////////////////////
		// public methods
		///////////////////////////////////
		/**
		 */
		public function setSliderParams(min:Number , max:Number , value:Number):void
		{
			_min = min;
			_max = max;
			_value = value;
			correctValue();
//			invalidate();
			draw();
		}

		/**
		 * 使用字符字节长度作为key
		 * 对应的bitmapData为value
		 */
//		private static const DIC_THUMB:Dictionary = new Dictionary;
//
//		private static function getCacheBitmapData(key:int):BitmapData
//		{
//			for (var i:* in DIC_THUMB)
//			{
//				if (DIC_THUMB[i] == key)
//					return i;
//			}
//			return null;
//		}

		private var drawHeight:int ;
		override public function draw():void
		{
			super.draw();
			var tH:int = thumb.height;
			dragRect.height = _height - tH;

//			var bmd:BitmapData = getCacheBitmapData(tH);
//			if (bmd == null)
//			{
//				bmd = new BitmapData(width , tH , true , 0);
//				Scale9BitmapData.drawScale3Vertical(bmd , thumbBitmapData , 4);
//				DIC_THUMB[bmd] = tH;
//			}
//			thumbIcon.bitmapData = bmd;
			if(drawHeight != tH)
			{
				drawHeight = tH;
				//thumb.height = tH;
//				thumb.graphics.clear();
//				Scale9Graphics.vScrollBarThumbScale3V.draw(thumb.graphics,_width,tH);
			}
			thumb.y = (_value - _min) * (height - tH) / (_max - _min);

			bg.height = _height;
//			graphics.clear();
//			graphics.beginFill(0 , 0);
//			graphics.drawRect(0 , 0 , 9 , _height);
//			graphics.beginFill(0 , .5);
//			graphics.drawRoundRect(3 , 0 , 3 , _height , 5);
//			graphics.endFill();
		}

//		/**
//		 * 自动隐现
//		 */
//		public function render():void
//		{
//			TweenLite.killTweensOf(this);
//			if (_needShow)
//			{
//				if (alpha != 1)
//					TweenMax.to(this , TIME_FADE_DURING , {autoAlpha: 1});
//				if (_isPersistenceShow == false)
//					TweenMax.to(this , TIME_FADE_DURING , {autoAlpha: 0 , delay: TIME_FADE_OUT_DELAY});
//			}
//			else if (visible)
//				TweenMax.to(this , TIME_FADE_DURING , {autoAlpha: 0});
//		}
		///////////////////////////////////
		// event
		///////////////////////////////////
		public var isScrolling:Boolean ;
		public var onHold:Function;

		private function dragThumbHanlder(e:MouseEvent):void
		{
			isScrolling = true;
			stage.addEventListener(MouseEvent.MOUSE_MOVE , onMove);
			stage.addEventListener(MouseEvent.MOUSE_UP , onDrop);
			thumb.startDrag(false , dragRect);
			if (onHold != null)
				onHold();
		}

		private function onMove(e:MouseEvent):void
		{
			var oldValue:Number = _value;
			value = thumb.y / dragRect.height * (_max - _min) + _min;
			if (_value != oldValue)
				onChange();
		}
		public var onRelease:Function;

		private function onDrop(e:MouseEvent):void
		{
			isScrolling = false;
			stage.removeEventListener(MouseEvent.MOUSE_UP , onDrop);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE , onMove);
			thumb.stopDrag();
			var oldValue:Number = _value;
			value = thumb.y / dragRect.height * (_max - _min) + _min;
			if (_value != oldValue)
				onChange();
			if (onRelease != null)
				onRelease();
		}

		private function clickHandler(e:MouseEvent):void
		{
			if (e.target == thumb)
				return;
			value = mouseY / dragRect.height * (_max - _min) + _min;
		}

		private function overThumbHanlder(e:MouseEvent):void
		{
			thumb.addEventListener(MouseEvent.ROLL_OUT , outThumbHandler);
			thumb.filters = Style.HOVER_FILTERS;
		}

		private function outThumbHandler(e:MouseEvent):void
		{
			thumb.removeEventListener(MouseEvent.ROLL_OUT , outThumbHandler);
			thumb.filters = null;
		}

		///////////////////////////////////
		// get/set
		///////////////////////////////////
		public function get max():Number
		{
			return _max;
		}

		public function set max(v:Number):void
		{
			if (_max == v)
				return;
			_max = v;
			correctValue();
//			invalidate();
			draw();
		}

		public function set value(v:Number):void
		{
			if (_value == v)
				return;
			_value = v;
			correctValue();
//			invalidate();
			draw();
		}

		public function get value():Number
		{
			return _value;
		}
		///////////////////////////////////
		// special
		///////////////////////////////////
//		private var _isPersistenceShow:Boolean    = true;
		private var _content:InteractiveObject;

		/**
		 * 特殊功能
		 * 捕获内容层
		 *
		 * 1.自动显示隐藏
		 * 鼠标移入时
		 * 2.监听鼠标滚轮
		 * onWheel = function (e:MouseEvent)
		 */
		public function catchContent(contentDO:InteractiveObject , autoFade:Boolean = true , onWheel:Function = null):void
		{
			_content = contentDO;
//			_isPersistenceShow = !autoFade;
//			render();
//			if (autoFade)
//				_content.addEventListener(MouseEvent.ROLL_OVER , overContentHandler);
			if (onWheel!= null)
			{
				_content.addEventListener(MouseEvent.MOUSE_WHEEL , onWheel);
			}
		}

//		private function overContentHandler(e:MouseEvent):void
//		{
//			_isPersistenceShow = true;
//			render();
//			_content.addEventListener(MouseEvent.ROLL_OUT , outContentHandler);
//		}
//
//		private function outContentHandler(e:MouseEvent):void
//		{
//			_isPersistenceShow = false;
//			render();
//			_content.removeEventListener(MouseEvent.ROLL_OUT , outContentHandler);
//		}

//		/**
//		 * 最大值 大于 一页的数目时
//		 * 滚动条才显示
//		 * 由外部控制
//		 */
//		public function set needShow(value:Boolean):void
//		{
//			_needShow = value;
//			render();
//		}

	}
}
